﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace RoketPack.Configuration
{
    public partial class CertificateInstallationPage : Wizard.UI.InternalWizardPage
    {
        public CertificateInstallationPage()
        {
            InitializeComponent();
        }

        private void CertificateInstallationPage_SetActive(object sender, CancelEventArgs e)
        {
            try
            {
                if (Certificate.IsInstalled())
                {
                    this.c_CertificateStatusLabel.Text = "The certificate is currently installed.";
                    this.c_CertificateStatusLabel.ForeColor = Color.ForestGreen;
                    this.c_CertificateInstallButton.Enabled = false;
                    this.SetWizardButtons(Wizard.UI.WizardButtons.Finish);
                }
                else
                {
                    this.c_CertificateStatusLabel.Text = "The certificate is not yet installed.";
                    this.c_CertificateStatusLabel.ForeColor = Color.Red;
                    this.c_CertificateInstallButton.Enabled = true;
                    this.SetWizardButtons(Wizard.UI.WizardButtons.None);
                }
            }
            catch (CryptographicException)
            {
                this.c_CertificateStatusLabel.Text = "Unable to detect the status of the certificate.  Ensure it is installed before continuing.";
                this.c_CertificateStatusLabel.ForeColor = Color.Red;
                this.c_CertificateInstallButton.Enabled = true;
                this.SetWizardButtons(Wizard.UI.WizardButtons.Finish);
            }
        }

        private void c_CertificateInstallButton_Click(object sender, EventArgs e)
        {
            this.c_CertificateStatusLabel.Text = "Waiting for confirmation...";
            this.c_CertificateStatusLabel.ForeColor = Color.Black;
            this.c_CertificateInstallButton.Enabled = false;
            this.SetWizardButtons(Wizard.UI.WizardButtons.None);
            this.Refresh();
            this.c_CertificateStatusLabel.Refresh();
            this.c_CertificateInstallButton.Refresh();

            if (Certificate.EnsureInstalled())
            {
                this.c_CertificateStatusLabel.Text = "The certificate is currently installed.";
                this.c_CertificateStatusLabel.ForeColor = Color.ForestGreen;
                this.c_CertificateInstallButton.Enabled = false;
                this.SetWizardButtons(Wizard.UI.WizardButtons.Finish);
            }
            else
            {
                this.c_CertificateStatusLabel.Text = "The user cancelled the installation of the certificate (or permission was denied).";
                this.c_CertificateStatusLabel.ForeColor = Color.Red;
                this.c_CertificateInstallButton.Enabled = true;
                this.SetWizardButtons(Wizard.UI.WizardButtons.None);
            }
        }
    }
}
